﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sklad1_2.Add.Update;
using Sklad1_2.Data;
using Sklad1_2.Default;

namespace Sklad1_2.Add
{
    public partial class AddProduct : Defaultform
    {
        public AddProduct()
        {
            InitializeComponent();
            Init();
        }

        public bool a = false;
        public override sealed void Init()
        {
            ABase.AddItem(comboBox1,"angar");
            if (comboBox1.SelectedValue == null)
            {
                MessageBox.Show("Заполните данные о Складах");
                Close();
            }
            else
            {
                a = true;
            }
            if (comboBox2.SelectedValue == null)
            {
                MessageBox.Show("Заполните данные о Категориях");
                Close();
            }
            else
            {
                a = true;
            }
            if (comboBox3.SelectedValue == null)
            {
                MessageBox.Show("Заполните данные о Фирмах");
                Close();
            }
            else
            {
                a = true;
            }
            ABase.AddItemDBGridView(dataGridView1,Helper.QueryProduct);
            Helper.Hidden(dataGridView1,0,1,2,3);
            
        }

        void Clear()
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }

        private void AddProduct_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox1.SelectedValue.GetType() == t)
            {
                ABase.AddItemCategory(comboBox2, Convert.ToInt32(comboBox1.SelectedValue));
                //if (comboBox2.SelectedValue == null)
                //{
                //    comboBox3.DataSource = null;
                //}
            }
            else
            {
                comboBox1.DataSource = null;
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox2.SelectedValue.GetType() == t)
            {
                ABase.AddItemFirma(comboBox3, Convert.ToInt32(comboBox2.SelectedValue));

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name=textBox1.Text,price = textBox2.Text, count = textBox3.Text;
            if (textBox1.Text.Length>0&&textBox2.Text.Length>0&&textBox3.Text.Length>0)
            {
                for (int i = 0; i < price.Length; i++)
                {
                    if (price[i] == ',')
                    {
                        price = price.Replace(',', '.');
                    }
                }
                for (int i = 0; i < count.Length; i++)
                {
                    if (count[i] == ',')
                    {
                        count = count.Replace(',', '.');
                    }
                }
                for (int i = 0; i < name.Length; i++)
                {
                    if (name[i] == ',')
                    {
                        name = name.Replace(',', '.');
                    }
                }
                ABase.InsertValueProduct(Convert.ToString(comboBox1.SelectedValue),Convert.ToString(comboBox2.SelectedValue),Convert.ToString(comboBox3.SelectedValue),name,price,count);
                Clear();
                Init();
            }

            else
            {
                MessageBox.Show("Заполните все поля ввода!!!");
            }

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            data=new List<string>();
            Helper.Db2List(dataGridView1,e,ref data,3,0,1,2,7,8,9);
            UpdateProduct aProduct=new UpdateProduct(data);
            aProduct.FormClosed += Bind;
            aProduct.ShowDialog();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
